import axios from 'axios'
import { ElMessage, ElMessageBox } from 'element-plus'

const http = axios.create({
    timeout:5000,
    baseURL:'https://lianghj.top:8888/api/private/v1/'
})

http.interceptors.request.use(config => {
    // let token = 'zbc9OcEN64dknFRio2acnSthDCpg99XjU7xdYO1FcrHZNqXmClbJZAYJKUlWSt1kqM'//汪宇涵是你爹
    let users = JSON.parse(localStorage.getItem('users'));
    console.log(users);
    if(users){
        // config.headers.token = token;
         config.headers.Authorization = users.token;
    }
    return config;
},err => {
    Promise.reject(err);
})

http.interceptors.response.use(res => {
    console.log(res,11)
    let status = res.data.meta.status;
    let msg = res.data.meta.msg;
    if(msg == "无效token"){
        ElMessageBox.confirm('token过期，是否跳转到登录页？','提示',{
            confirmButtonText:'确定',
            cancelButtonText:'取消',    
            type:'warning'
        }).then(()=>{
            location.href = "/login"
        }).catch(()=>{
        })
    }else if ( status == 400 || status == 401 || status == 403 || status == 404 || status == 500 ){
        ElMessage.error({
            duration:1000,
            message:msg
        })
    }
    
    return res.data;
},err => {
//你记住你说的话就行
    let {message} = err; //结构出错误信息
    if(message.includes('timeout')){
        message="系统接口请求超时"
    }
    if(message == "Error"){
        message = "后端接口连接Network异常";
    }
    ElMessage.error({
        duration:1000,
        message:message
    })
    process.abort(edd)   
    Promise.reject(err)
})
